package org.thanos.simulator.page.network;

import org.thanos.simulator.api.MQTTClientApi;
import org.thanos.simulator.page.IndexPage;

import javax.swing.*;
import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.text.NumberFormat;

public class NetworkConfigPage {
    private final JTextField brokerUri;
    private final JTextField clientId;
    private final JTextField username;
    private final JTextField password;

    public NetworkConfigPage(IndexPage indexPage) {
        // 创建 JFrame
        JFrame frame = new JFrame("本地网络配置");
        frame.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new GridBagLayout()); // 使用 GridBagLayout

        // 创建 GridBagConstraints
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5, 5, 5, 5); // 设置组件之间的间距

        // 创建数字输入框
        NumberFormat format = NumberFormat.getInstance();
        NumberFormatter formatter = new NumberFormatter(format);
        formatter.setValueClass(Integer.class);
        formatter.setMinimum(0);
        formatter.setMaximum(Integer.MAX_VALUE);

        gbc.gridx = 0;
        gbc.gridy++;
        frame.add(new JLabel("BrokerUri:"), gbc); // 添加标签
        gbc.gridx++;
        brokerUri = new JTextField();
        brokerUri.setText("tcp://127.0.0.1:1883");
        brokerUri.setPreferredSize(new Dimension(150, 30)); // 设置尺寸为 150x30 像素
        frame.add(brokerUri, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        frame.add(new JLabel("ClientId:"), gbc); // 添加标签
        gbc.gridx++;
        clientId = new JTextField();
        clientId.setPreferredSize(new Dimension(150, 30)); // 设置尺寸为 150x30 像素
        frame.add(clientId, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        frame.add(new JLabel("Username:"), gbc); // 添加标签
        gbc.gridx++;
        username = new JTextField();
        username.setText("admin");
        username.setPreferredSize(new Dimension(150, 30)); // 设置尺寸为 150x30 像素
        frame.add(username, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        frame.add(new JLabel("Password:"), gbc); // 添加标签
        gbc.gridx++;
        password = new JTextField();
        password.setText("123456");
        password.setPreferredSize(new Dimension(150, 30)); // 设置尺寸为 150x30 像素
        frame.add(password, gbc);

        // 创建确认按钮
        gbc.gridx = 0;
        gbc.gridy++;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        JButton button = new JButton("确认");
        button.addActionListener(e -> {
            try {
                String brokerValue = brokerUri.getText();
                String clientIdValue = clientId.getText();
                String usernameValue = username.getText();
                String passwordValue = password.getText();
                MQTTClientApi.init(brokerValue, clientIdValue, usernameValue, passwordValue);
                JOptionPane.showMessageDialog(null, "连接成功！");
                indexPage.reloadMenu();
                frame.dispose();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
                throw new RuntimeException(ex);
            }
        });
        frame.add(button, gbc);
        frame.pack(); // 自适应大小
        frame.setVisible(true);
    }
}
